package com.zxkj.service;

import com.zxkj.domain.entity.SciDoc;
import com.zxkj.domain.mapper.SciDocRepository;
import com.zxkj.mssql.entity.UtInfo;
import com.zxkj.mssql.repository.UtInfoRepository;
import com.zxkj.utils.ReadHtmlHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/5/16.
 */
@Service
public class UtInfoService {
    private static final Logger logger = LoggerFactory.getLogger(UtInfoService.class.getName());
/*    @Autowired
    private UtInfoRepository utInfoRepository;*/
    @Autowired
    private SciDocRepository sciDocRepository;

    private int pageSize = 10000;

    /**
     * 将mysql中的doc表导入mssql中
     */
    public void mysql2Mssql(Integer nowpage, Integer endPage) {
        while (true) {

            logger.warn("当前第" + (nowpage + 1) + "页");

            List<SciDoc> sciList = findAllSciDoc(nowpage, endPage);

            if (sciList != null) {
                for (SciDoc sciDoc : sciList) {
                    UtInfo utInfo = new UtInfo();
                    utInfo.setUt(sciDoc.getUt());
                    utInfo.setNum(sciDoc.getNum());
                    String html = sciDoc.getHtml();
                    utInfo.setDoi(ReadHtmlHelper.GetRawValue(html, "DI"));
                    utInfo.setPmid(ReadHtmlHelper.GetRawValue(html, "PM"));
//                    utInfoRepository.save(utInfo);
                }
            }

            if (nowpage.equals(endPage)) {
                logger.warn("最后一页已完成");
                break;
            }
            nowpage++;
        }
    }

    private List<SciDoc> findAllSciDoc(Integer nowpage, Integer endPage) {
        if (nowpage.compareTo(endPage) > 0 && endPage.compareTo(0) > 0) {
            return null;
        }
        int startNum = nowpage * pageSize + 1;
        int endNum = (nowpage + 1) * pageSize;
        return sciDocRepository.findAll(startNum, endNum);
    }

    /**
     * 将mysql中的doc表导入文件中
     */
    public void mysql2File(Integer nowpage, Integer endPage) throws IOException {
        File file = new File(nowpage + "-" + endPage + ".csv");
        if (!file.exists()) {
            file.createNewFile();
        }
        OutputStream out = new FileOutputStream(file);
        while (true) {

            logger.warn("当前第" + (nowpage + 1) + "页");

            List<SciDoc> sciList = findAllSciDoc(nowpage, endPage);

            if (sciList != null) {
                for (SciDoc sciDoc : sciList) {
                    String html = sciDoc.getHtml();
                    StringBuilder sb = new StringBuilder();
                    sb.append(sciDoc.getNum() + ",");
                    sb.append(sciDoc.getUt() + ",");
                    sb.append(ReadHtmlHelper.GetRawValue(html, "DI") + ",");
                    sb.append(ReadHtmlHelper.GetRawValue(html, "PM")+"\n");
                    out.write(sb.toString().getBytes());
                }
            }

            if (nowpage.equals(endPage)) {
                logger.warn("最后一页已完成");
                break;
            }
            nowpage++;
        }
        out.close();
    }

}
